const app = getApp()
const address = require("../../api/address.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarData: {
      showCapsule: true, //是否显示左上角图标
      title: '修改地址', //导航栏 中间的标题
      isHome: false
    },
    isAdd: false,
    region: ['四川省', '成都市', '武侯区'],
    height: app.globalData.height * 2 + 20 ,  
    addre: {},
    receiverName: "",
    receiverTelephone: "",
    street: '',
    streetSize: 0,
    rules: [{
      name: 'receiverTelephone',
      rules: {required: true, message: '手机号码必填'},
      }, {
          name: 'province',
          rules: {required: true, message: '配送地址必填'},
      }, {
        name: 'receiverName',
        rules: {required: true, message: '收货人必填'}
    }, {
      name: 'street',
      rules: {required: true, message: '街道必填'}
    }]
  },
  inputChange: function(e){
    const {field} = e.currentTarget.dataset
    var value = e.detail.value
    if (field == 'street'){
      var i = value.length
      if (i >= 100){
        wx.showToast({
          title: '输入超出限制',
          icon: 'none'
        })
        this.setData(
          {street: this.data.street}
        )
        return
      }
      this.setData({
        street: value,
        streetSize: value.length
      })
    }
    this.setData({
        [`addre.${field}`]: value
    })
  },
  bindRegionChange: function(e){
    var ad = this.data.addre
    ad["province"] = e.detail.value[0]
    ad["city"] = e.detail.value[1]
    ad["area"] = e.detail.value[2]
    
    this.setData({
      region: e.detail.value,
      addre: ad
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      address.find(options.id).then(res =>{
        var i = res.data
        this.setData({
          addre: i,
          region: [i.province, i.city, i.area],
          receiverName: i.receiverName,
          receiverTelephone: i.receiverTelephone,
          street: i.street,
          streetSize: i.street.length
        })
      })
    }else{
      var i = this.data.navbarData
      i.title = "新增地址"
      this.setData({
        navbarData: i,
        isAdd: true,
        addre: {province: this.data.region[0], city: this.data.region[1], area: this.data.region[2]}
      })
    }
    
  },
  submitForm() {
    if (!this.data.addre.receiverName){
      wx.showToast({
        title: '收货人不能为空',
        icon: 'none'
      })
      return
    }
    if (!this.data.addre.receiverTelephone){
      wx.showToast({
        title: '电话号码不能为空',
        icon: 'none'
      })
      return
    }
    if (!this.data.addre.province){
      wx.showToast({
        title: '配送地址不能为空',
        icon: 'none'
      })
      return
    }
    if (!this.data.addre.street){
      wx.showToast({
        title: '街道不能为空',
        icon: 'none'
      })
      return
    }
    this.selectComponent('#form').validate((valid, errors) => {
      console.log('valid', valid, errors)
        if (!valid) {
          var err = errors[0].message
            wx.showToast({
              title: err,
              icon: 'none'
            })
            return
        } else {
          if(!this.data.isAdd){
            address.edit(this.data.addre).then(res=>{
              wx.navigateBack({
                delta: 0,
              })
              wx.showToast({
                title: '修改成功',
                icon: 'none'
              })
            })
          }else{
            address.add(this.data.addre).then(res=>{
              wx.navigateBack({
                delta: 0,
              })
              wx.showToast({
                title: '添加成功',
                icon: 'none'
              })
            })
          }
        }
    })
  },
  getLocation() {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})